{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.64651822, 0.18254282, 0.23597111, 0.97006303, 0.65763728,\n",
       "        0.93085118],\n",
       "       [0.0585817 , 0.68406579, 0.72719266, 0.98342701, 0.41624125,\n",
       "        0.32608285],\n",
       "       [0.2344893 , 0.0628827 , 0.2669825 , 0.41319709, 0.44508428,\n",
       "        0.2389589 ],\n",
       "       [0.1275134 , 0.69837215, 0.03413229, 0.25314946, 0.07587752,\n",
       "        0.6243547 ],\n",
       "       [0.09317721, 0.93373972, 0.17613588, 0.50404854, 0.30653522,\n",
       "        0.45499094],\n",
       "       [0.18010479, 0.33043062, 0.45752312, 0.3512856 , 0.67076134,\n",
       "        0.91191093]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.rand(6,6)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.64651822, 0.65763728],\n",
       "       [0.2344893 , 0.44508428],\n",
       "       [0.09317721, 0.30653522]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2,::4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(12)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.numel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = x.reshape(3,4)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros((3,2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]]],\n",
       "\n",
       "\n",
       "        [[[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]]],\n",
       "\n",
       "\n",
       "        [[[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "         [[1., 1., 1., 1., 1., 1.],\n",
       "          [1., 1., 1., 1., 1., 1.]]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones((3,5,2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6465, 0.1825, 0.2360, 0.9701, 0.6576, 0.9309],\n",
       "        [0.0586, 0.6841, 0.7272, 0.9834, 0.4162, 0.3261],\n",
       "        [0.2345, 0.0629, 0.2670, 0.4132, 0.4451, 0.2390],\n",
       "        [0.1275, 0.6984, 0.0341, 0.2531, 0.0759, 0.6244],\n",
       "        [0.0932, 0.9337, 0.1761, 0.5040, 0.3065, 0.4550],\n",
       "        [0.1801, 0.3304, 0.4575, 0.3513, 0.6708, 0.9119]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor(a,dtype=torch.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多张量合并连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0,  1,  2,  3,  4],\n",
       "         [ 5,  6,  7,  8,  9],\n",
       "         [10, 11, 12, 13, 14],\n",
       "         [ 2,  1,  3,  4,  5],\n",
       "         [ 5,  6,  7,  4,  0],\n",
       "         [ 9,  3,  8,  5,  1]]),\n",
       " tensor([[ 0,  1,  2,  3,  4,  2,  1,  3,  4,  5],\n",
       "         [ 5,  6,  7,  8,  9,  5,  6,  7,  4,  0],\n",
       "         [10, 11, 12, 13, 14,  9,  3,  8,  5,  1]]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(15).reshape(3,5)\n",
    "y = torch.tensor([[2,1,3,4,5],[5,6,7,4,0],[9,3,8,5,1]])\n",
    "torch.cat((x, y), dim=0), torch.cat((x, y), dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False,  True, False, False, False],\n",
       "        [ True,  True,  True, False, False],\n",
       "        [False, False, False, False, False]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(105)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **操作导致新结果分配内存**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(x)\n",
    "x = x + 1\n",
    "id(x) == before"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如果后续计算中不需要重复使用`x`，可以使用`x[:] = x + y`或`x += y`来减少操作的内存开销"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(x)\n",
    "x[:] = x + 1\n",
    "id(x) == before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(x)\n",
    "x += 1\n",
    "id(x) == before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.ndarray, torch.Tensor)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = x.numpy()\n",
    "B = torch.tensor(A)\n",
    "type(A), type(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(10), 10, 10.0)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1,2], x[1,2].item(), float(x[1,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs(os.path.join('..', 'data'), exist_ok = True)\n",
    "data_file = os.path.join('..', 'data', 'house_haha.csv')\n",
    "with open(data_file, 'w') as f:\n",
    "    f.write('NumRooms,Alley,Price\\n')    #列明\n",
    "    f.write('2,demo1,1245035\\n')     #一个样本\n",
    "    f.write('NA,NA,5451354\\n')\n",
    "    f.write('4,NA,4512146\\n')\n",
    "    f.write('NA,demo2,2348213')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   NumRooms  Alley    Price\n",
      "0       2.0  demo1  1245035\n",
      "1       NaN    NaN  5451354\n",
      "2       4.0    NaN  4512146\n",
      "3       NaN  demo2  2348213\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv(data_file)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 为了处理缺失的数据，典型的方法包括 插值 和 删除， 这里，我们将考虑插值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   NumRooms  Alley\n",
      "0       2.0  demo1\n",
      "1       3.0    NaN\n",
      "2       4.0    NaN\n",
      "3       3.0  demo2\n"
     ]
    }
   ],
   "source": [
    "inputs, outputs = data.iloc[:,:2], data.iloc[:,2]\n",
    "inputs = inputs.fillna(inputs.mean())\n",
    "print(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对于离散数据或类别值，将“NaN”转为一个类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   NumRooms  Alley_demo1  Alley_demo2  Alley_nan\n",
      "0       2.0            1            0          0\n",
      "1       3.0            0            0          1\n",
      "2       4.0            0            0          1\n",
      "3       3.0            0            1          0\n"
     ]
    }
   ],
   "source": [
    "inputs = pd.get_dummies(inputs, dummy_na=True)\n",
    "print(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在 inputs 和 outputs 中的所有条目都是数值类型，它们可以转换为张量格式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[2., 1., 0., 0.],\n",
       "         [3., 0., 0., 1.],\n",
       "         [4., 0., 0., 1.],\n",
       "         [3., 0., 1., 0.]], dtype=torch.float64),\n",
       " tensor([1245035, 5451354, 4512146, 2348213]))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X,y = torch.tensor(inputs.values), torch.tensor(outputs.values)\n",
    "X, y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
